import java.util.List;
import org.junit.Test;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.zero.model.Student;

/**
 * @author Zero
 */
public class Demo1 {
    @Test
    public void test() {
        //设置数据源-使用 Spring 框架自带的 DriverManagerDataSource，当然也可以使用Druid，C3P0，JNDI(Web)等等
        DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
        driverManagerDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        driverManagerDataSource.setUrl("jdbc:mysql://localhost:3306/learn_ssm?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC");
        driverManagerDataSource.setUsername("zero");
        driverManagerDataSource.setPassword("abc123");

        //获取 JdbcTemplate 对象
        JdbcTemplate jdbcTemplate = new JdbcTemplate(driverManagerDataSource);

        //执行方法
        //坑，此方法不支持自定义bean，传入class只支持Integer,String这些基本类型
        // List<Student> students = jdbcTemplate.queryForList("select * from student", Student.class);
        List<Student> students = jdbcTemplate.query("select * from student",
                new BeanPropertyRowMapper<>(Student.class));

        students.forEach(System.out::println);
    }
}
